{# SPDX-FileCopyrightText: © 2019 Sandip Kumar Bhuyan <sandipbhuyan@gmail.com>
   Author: Sandip Kumar Bhuyan(sandipbhuyan@gmail.com)

   SPDX-License-Identifier: FSFAP
#}

function createDataTable() {
    var dirlistTableConfig = {
        "bServerSide": true,
        "sAjaxSource": "?mod=ajax_sh_browser",
        "fnServerData": function (sSource, aoData, fnCallback) {
            aoData.push({"name": "upload", "value": {{ uploadId }} });
            aoData.push({"name": "item", "value": {{ itemId }} });
            aoData.push({"name": "totalRecords", "value": {{ iTotalRecords }} });
            aoData.push({"name": "agentId", "value": {{ agentId|default(0) }} });
            aoData.push({"name": "scanFilter", "value": $('#scanFilter').val() });
            {% if isFlat %}aoData.push({"name": "flatten", "value": "yes" });{% endif %}
            $.getJSON(sSource, aoData, fnCallback)
                .fail(failed)
        },
        "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
            if (nRow.className.match(/(^| )removed($| )/g)  ) {
                $('td', nRow).addClass('read_only');
            }
        },
        "aoColumns": [{"sTitle":"Files","sClass":"left","bSearchable":true},
            {"sTitle":"Hash Value(SHA256)","sClass":"left","bSortable":false,"bSearchable":false},
            {"sTitle":"License (Software Heritage)","sClass":"left","bSortable":false},
            {"sTitle":"Software Heritage Status","sClass":"center","bSortable":false},
            {"sTitle":"Actions","sClass":"left","bSortable":false,"bSearchable":false}],
        "sPaginationType": "listbox",
        "bProcessing": true,
        "bStateSave": true,
        "bRetrieve": true,
        "bPaginate": true,
        "bFilter": true,
        "bAutoWidth": true,
        "iDisplayLength":50,
        "oLanguage":{"sInfo":"Showing _START_ to _END_ of _TOTAL_ files" ,
            "sSearch":'_INPUT_<button onclick="clearSearchFiles();" title=\"{{ 'Clear file filter'|trans }}\" >Clear<\/button>',
            "sLengthMenu":'Display <select><option value="10">10<\/option><option value="25">25<\/option><option value="50">50<\/option><option value="100">100<\/option><\/select> {#
       #}   {{ 'files'|trans }} ({% if isFlat %}<a href="{{ fileSwitch }}">{{ 'tree view'|trans }}</a> {{ 'or'|trans }} <b>{{ 'flat'|trans }}</b>{#
       #}{% else %}<b>{{ 'tree view'|trans }}</b> {{ 'or'|trans }} <a href="{{ fileSwitch }}">{{ 'flat'|trans }}</a>{% endif %})'
        }
    };

    otable = $('#dirlist').dataTable(dirlistTableConfig);
}

$(document).ready( function () {
    createDataTable();
} );
